package ru.isys.view;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;

public class MainView extends JFrame
{

    JPanel modelPanel;
    JPanel requestResponsePanel;
    SpringLayout modelPanelLayout;
    GridBagConstraints modelPanelConstraints;
    JScrollPane centralScrollPanel;
    JScrollPane westScrollPanel;
    FileURIAndNsPanel fileNNsPanel;
    RequestResponsePanel reqResPanel;
    SpringLayout layout;

    public MainView(FileURIAndNsPanel panel)
    {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(dim);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        layout = new SpringLayout();
        setLayout(layout);
        prepareFileToOpenPanel(panel);
        prepareResponseRequestPanel();
        prepareModelPanel();
        prepareWestScrollPanel();

        reqResPanel.setVisible(false);
        modelPanel.setVisible(false);
        westScrollPanel.setVisible(false);
        setLayoutConstrains();
        validate();

        // initHandlers();
    }

    private void setLayoutConstrains()
    {
        layout.putConstraint(SpringLayout.WEST, fileNNsPanel, 10, SpringLayout.WEST, this.getContentPane());
        layout.putConstraint(SpringLayout.NORTH, fileNNsPanel, 0, SpringLayout.NORTH, this.getContentPane());
        layout.putConstraint(SpringLayout.EAST, this.getContentPane(), 10, SpringLayout.EAST, fileNNsPanel);
        layout.putConstraint(SpringLayout.SOUTH, fileNNsPanel, 100, SpringLayout.NORTH, this.getContentPane());

        System.out.println(fileNNsPanel.getHeight());
        layout.putConstraint(SpringLayout.WEST, reqResPanel, 10, SpringLayout.WEST, this.getContentPane());
        layout.putConstraint(SpringLayout.NORTH, reqResPanel, -100, SpringLayout.SOUTH, this.getContentPane());
        layout.putConstraint(SpringLayout.EAST, reqResPanel, 10, SpringLayout.EAST, fileNNsPanel);
        layout.putConstraint(SpringLayout.SOUTH, reqResPanel, 100, SpringLayout.NORTH, reqResPanel);

        layout.putConstraint(SpringLayout.WEST, westScrollPanel, 10, SpringLayout.WEST, this.getContentPane());
        layout.putConstraint(SpringLayout.NORTH, westScrollPanel, 0, SpringLayout.SOUTH, fileNNsPanel);
        layout.putConstraint(SpringLayout.EAST, westScrollPanel, -800, SpringLayout.EAST, fileNNsPanel);
        layout.putConstraint(SpringLayout.SOUTH, westScrollPanel, -10, SpringLayout.NORTH, reqResPanel);

        // layout.putConstraint(SpringLayout.WEST, responseRadioButton, 0, SpringLayout.WEST, requestRadioButton);
        // layout.putConstraint(SpringLayout.EAST, responseRadioButton, 0, SpringLayout.EAST, requestRadioButton);
        // layout.putConstraint(SpringLayout.NORTH, requestRadioButton, 5, SpringLayout.NORTH, this);
        // layout.putConstraint(SpringLayout.NORTH, responseRadioButton, 5, SpringLayout.SOUTH, requestRadioButton);
    }

    private void prepareModelPanel()
    {
        modelPanel = new JPanel();
        modelPanelLayout = new SpringLayout();
       // modelPanel.setLayout(modelPanelLayout);
    }

    private void prepareWestScrollPanel()
    {
        westScrollPanel = new JScrollPane(modelPanel);
        getContentPane().add(westScrollPanel);
    }

    private void prepareFileToOpenPanel(FileURIAndNsPanel panel)
    {
        this.fileNNsPanel = panel;

        getContentPane().add(this.fileNNsPanel);
    }

    private void prepareResponseRequestPanel()
    {
        reqResPanel = new RequestResponsePanel();

        getContentPane().add(this.reqResPanel);

    }

    public void addModel(JPanel modelPanel)
    {
        if (centralScrollPanel != null)
        {
            System.out.println ("centralScrollPanel removed");
            remove(centralScrollPanel);
        }
        centralScrollPanel = new JScrollPane(modelPanel);
        layout.putConstraint(SpringLayout.WEST, centralScrollPanel, 10, SpringLayout.EAST, westScrollPanel);
        layout.putConstraint(SpringLayout.NORTH, centralScrollPanel, 0, SpringLayout.NORTH, westScrollPanel);
        layout.putConstraint(SpringLayout.EAST, centralScrollPanel, -10, SpringLayout.EAST, fileNNsPanel);
        layout.putConstraint(SpringLayout.SOUTH, centralScrollPanel, 0, SpringLayout.SOUTH, westScrollPanel);
        add(centralScrollPanel);
        validate();
    }

    public void removeFileToOpenPanel()
    {

        modelPanel.setVisible(true);
        reqResPanel.setVisible(true);
        westScrollPanel.setVisible(true);
        this.getContentPane().invalidate();
        this.getContentPane().validate();
    }

    public void addInfo(String info)
    {
        JTextArea message = new JTextArea(info);
        message.setFont(new Font("Arial", Font.PLAIN, 12));
        
        modelPanel.add(message);
        //modelPanelLayout.putConstraint(SpringLayout.NORTH, message, 0, SpringLayout.NORTH, westScrollPanel);
        //modelPanelLayout.putConstraint(SpringLayout.SOUTH, message, 0, SpringLayout.SOUTH, westScrollPanel);
       // modelPanelLayout.putConstraint(SpringLayout.EAST, message, 0, SpringLayout.EAST, westScrollPanel);
       // modelPanelLayout.putConstraint(SpringLayout.WEST, message, 0, SpringLayout.WEST, westScrollPanel);
        validate();
    }

    private void initHandlers()
    {
        addComponentListener(new ComponentListener()
        {

            @Override
            public void componentResized(ComponentEvent e)
            {
                if (centralScrollPanel != null)
                {
                    //centralScrollPanel.setBounds(350, 0, getWidth() - 365, getHeight() - 50);
                }

                // westScrollPanel.setBounds(0, 0, 350, getHeight() - 50);
                validate();
            }

            @Override
            public void componentMoved(ComponentEvent e)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void componentShown(ComponentEvent e)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void componentHidden(ComponentEvent e)
            {
                // TODO Auto-generated method stub

            }

        });
    }
}
